package com.hqq.persistence_tier.data_mapper;

import java.util.*;

/**
 * StudentDataMapperImpl
 * Created by heqianqian on 2017/8/14.
 */
public class StudentDataMapperImpl implements StudentDataMapper {

    private Map<Integer, Student> studentMap = new HashMap<>();

    @Override
    public Optional<Student> find(int studentId) {
        for (Integer id : studentMap.keySet()) {
            if (id == studentId) {
                return Optional.of(studentMap.get(id));
            }
        }
        return Optional.empty();
    }

    @Override
    public void update(Student student) {
        for (Integer id : studentMap.keySet()) {
            if (id == student.getStudentId()) {
                studentMap.put(id, student);
                return;
            }
        }
        throw new StudentDataException();
    }

    @Override
    public void insert(Student student) {
        studentMap.put(student.getStudentId(), student);
    }

    @Override
    public void delete(Student student) {
        for (Integer id : studentMap.keySet()) {
            if (id == student.getStudentId()) {
                studentMap.remove(id);
            }
        }
    }

    @Override
    public List<Student> getStudents() {
        return new ArrayList<>(studentMap.values());
    }
}
